{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10001 10001\n",
      "Good day !\n"
     ]
    }
   ],
   "source": [
    "# read file\n",
    "with open(\"rmsd.xvg\", 'r') as fo:\n",
    "    lines = fo.readlines()\n",
    "\n",
    "    # deal with content of file\n",
    "xaxis,yaxis = \"\",\"\"\n",
    "time_list, rmsd_list = [],[]\n",
    "\n",
    "for line in lines:\n",
    "    if line[0] == \"#\":\n",
    "        continue\n",
    "    if line[0] == \"@\":\n",
    "        if \"xaxis\" in line:\n",
    "            xaxis = line.strip().strip('\"').split('\"')[-1]\n",
    "        if \"yaxis\" in line:\n",
    "            yaxis = line.strip().strip('\"').split('\"')[-1]\n",
    "    \n",
    "#    print(xaxis,yaxis)\n",
    "            \n",
    "# deal with data line\n",
    "    if not line.startswith(\"@\") and not line.startswith(\"#\"):\n",
    "        items = line.strip().split()\n",
    "        time_list.append(float(items[0]))\n",
    "        rmsd_list.append(float(items[1]))\n",
    "#     print(time_list,rmsd_list)\n",
    "\n",
    "# check length of time_list, rmsd_list\n",
    "if len(time_list) != len(rmsd_list):\n",
    "    print(len(time_list), len(rmsd_list),'\\n', \"Wrong in length time_list and rmsd_list\")\n",
    "# print(len(time_list),len(rmsd_list)) \n",
    "\n",
    "# save csv file\n",
    "with open(\"rmsd.csv\", 'w') as fo :\n",
    "    fo.write(xaxis + \",\" +yaxis+'\\n')\n",
    "    for i in range(len(time_list)):\n",
    "        fo.write(\"{},{}\\n\".format(time_list[i], rmsd_list[i]))\n",
    "       \n",
    "print(\"Good day !\")\n",
    "            \n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
